﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using ToolRental.Client.Customer.CustomerService;

namespace ToolRental.Client.Customer
{
    public class ViewModel : INotifyPropertyChanged
    {

        #region UserId
        private int userId;
        public int UserId
        {
            get
            {
                return userId;
            }
            set
            {
                userId = value;
                PropChanged("UserId");
            }
        }
        #endregion

        #region ToolTypes
        private ObservableCollection<Model.ToolType> toolTypes;
        public ObservableCollection<Model.ToolType> ToolTypes
        {
            set
            {
                toolTypes = value;
                PropChanged("ToolTypes");
            }
            get
            {
                if (toolTypes == null)
                    toolTypes = new ObservableCollection<Model.ToolType>();
                return toolTypes;
            }
        }
        #endregion

        #region UpdateOrderListItemsCommand

        private ICommand updateOrderListItemsCommand;
        public ICommand UpdateOrderListItemsCommand
        {
            get
            {
                if (updateOrderListItemsCommand == null)
                    updateOrderListItemsCommand = new CustomCommand(UpdateOrderListItems);
                return updateOrderListItemsCommand;
            }
        }

        private async void UpdateOrderListItems()
        {
            try
            {
                OrderListElement[] orderListElements;
                using (var asc = new AdminServiceClient())
                {
                    orderListElements = await asc.GetOrderListElementsAsync(UserId);
                }

                var newToolTypes = new ObservableCollection<Model.ToolType>();
                foreach (var ole in orderListElements)
                {
                    newToolTypes.Add(new Model.ToolType()
                        {
                            Availability = ole.Availability,
                            BrandName = ole.BrandName,
                            Id = ole.Id,
                            TypeName = ole.TypeName,
                            Borrowed = ole.Borrowed,
                            Ordered = ole.Ordered
                        });
                }

                ToolTypes = newToolTypes;
            }
            catch (Exception e)
            {
                ServiceIsUnavailable(e.Message);
            }
        }
 
        #endregion

        #region DoOrderCommand
        private ICommand doOrderCommand;
        public ICommand DoOrderCommand
        {
            get
            {
                if (doOrderCommand == null)
                    doOrderCommand = new CustomCommand(DoOrder);
                return doOrderCommand;
            }
        }

        private async void DoOrder()
        {
            int selectedId = 0;
            try
            {
                selectedId = ToolTypes.Where(p => (p.IsChecked == true)).First().Id;
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.Message);
                MessageBox.Show("Nincs kiválasztva elem!");
            }
            if (selectedId > 0)
            {
                try
                {
                    using (var asc = new AdminServiceClient())
                    {
                        await asc.NewOrderAsync(selectedId, UserId);
                    }
                    UpdateOrderListItems();
                }
                catch (Exception e)
                {
                    ServiceIsUnavailable(e.Message);
                }
            }
        }

        #endregion

        #region INotifyPropertyChanged interface
        public event PropertyChangedEventHandler PropertyChanged;

        private void PropChanged(string prop)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(prop));
        }
        #endregion

        #region public constructor
        public ViewModel()
        {
            CustomCommand.IsEnabled = true;
            UserId = 1;
            UpdateOrderListItems();
        }
        #endregion

        #region Service is unavailable
        private void ServiceIsUnavailable(string message)
        {
            Debug.WriteLine(message);
            MessageBox.Show("A kért szolgáltatás nem elérhető");
        }
        #endregion
    }
}
